package com.test.latitudex.utilities;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.util.Log;

public class NetworkManager {

	public static final int PUT = 1;
	public static final int GET = 2;
	public static final int POST = 3;
	public static final int DELETE = 4;
	private static final String LOG_TAG = "NetworkManager";
	private static NetworkManager entity = null;
	private static DefaultHttpClient httpClient;
	private static HttpRequestListener rListener;
	
	protected NetworkManager() {

	}

	public static NetworkManager getEntity() {
		if (entity == null) {
			entity = new NetworkManager();
		}
		return entity;
	}

	public void setHttpClientAuth(String user,String pass){
		httpClient = new DefaultHttpClient();
		if(!("".equals(user) || user == null || "".equals(pass) || pass == null)){
			httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
					new UsernamePasswordCredentials(user, pass));
		}
	}
	
	public void setHttpRequestListener(HttpRequestListener rListener){
		NetworkManager.rListener = rListener;
	}
	
	public void makeRequest(final String URL, final Map<?, ?> body, final Header[] headers, final int method) {
		Log.i(LOG_TAG, "Request sent");
		new Thread(new Runnable() {
			public void run() {
				switch (method) {
				case PUT: {
					rListener.onHttpResult(makePutRequest(URL, body, headers));
				}break;
				case GET: {
					rListener.onHttpResult(makeGetRequest(URL, headers));
				}break;
				case POST: {
					rListener.onHttpResult(makePostRequest(URL, body, headers));
				}break;
				case DELETE: {
					rListener.onHttpResult(makeDeleteRequest(URL));
				}break;
				default: {
					rListener.onHttpResult(null);
				}break;
				}
			}
		}).start();
	}

	private HttpResponse makeDeleteRequest(String URL) {
		try {
			return httpClient.execute(new HttpDelete(URL));
		} catch (ClientProtocolException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		} catch (IOException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		}
	}

	private HttpResponse makePostRequest(String URL, Map<?, ?> body, Header[] headers) {

		HttpPost p = new HttpPost(URL);
		if (body != null) {
			JSONObject json = new JSONObject(body);
			StringEntity se;
			try {
				se = new StringEntity(json.toString());
				p.setEntity(se);
			} catch (UnsupportedEncodingException e) {
				Log.e(LOG_TAG, e.getLocalizedMessage());
				return null;
			}
		}

		if (headers != null) {
			p.setHeaders(headers);
		}
		try {
			return httpClient.execute(p);
		} catch (ClientProtocolException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		} catch (IOException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		}

	}

	private HttpResponse makeGetRequest(String URL, Header[] headers) {
		HttpGet p = new HttpGet(URL);
		if (headers != null) {
			p.setHeaders(headers);
		}
		try {
			return httpClient.execute(p);
		} catch (ClientProtocolException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		} catch (IOException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		}

	}

	private HttpResponse makePutRequest(String URL, Map<?, ?> body, Header[] headers) {
		HttpPut p = new HttpPut(URL);
		if (body != null) {
			JSONObject json = new JSONObject(body);
			StringEntity se;
			try {
				se = new StringEntity(json.toString());
				p.setEntity(se);
			} catch (UnsupportedEncodingException e) {
				Log.e(LOG_TAG, e.getLocalizedMessage());
				return null;
			}
		}

		if (headers != null) {
			p.setHeaders(headers);
		}
		try {
			return httpClient.execute(p);
		} catch (ClientProtocolException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		} catch (IOException e) {
			Log.e(LOG_TAG, e.getLocalizedMessage());
			return null;
		}

	}
	
	public interface HttpRequestListener{
		public void onHttpResult(HttpResponse response);
	}

}
